# 实现一些自定义装饰器，如登录验证等
from functools import wraps

from flask import g
from utils.ResponseResult import fail_json_result

"""
登录验证装饰器：在不需要校验是否登录的接口方法上添加
@not_login_required即可

如果需要校验，则需要修改逻辑
"""


def not_login_required(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        if not g.user:
            return func(*args, **kwargs)
        # else:
        #     return fail_json_result(msg="用户未登录,请先登录")

    wrapper.is_custom_decorated = True
    return wrapper


# 登录校验
def login_required(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        if g.user:
            return func(*args, **kwargs)
        else:
            return fail_json_result(msg="用户未登录,请先登录")

    wrapper.is_custom_decorated = True
    return wrapper
